package com.zb.service.impl;

import cn.dev33.satoken.secure.SaSecureUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.AccounLoginInfo;
import com.zb.entity.ZjAccount;
import com.zb.mapper.ZjAccountMapper;
import com.zb.secure.BCryptUtil;
import com.zb.service.ZjAccountService;
import com.zb.utils.VerificUtil;
import com.zb.vo.AccountInfoVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

/**
 * <p>
 * 账号表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-07-18
 */
@Service
public class ZjAccountServiceImpl extends ServiceImpl<ZjAccountMapper, ZjAccount> implements ZjAccountService {
    @Resource
    private ZjAccountMapper accountMapper;

    // 手机号是否已经被注册
    @Override
    public boolean existByPhone(String phone) {
        return null != accountMapper.existByPhone(phone);
    }

    // 根据手机号获取账号信息
    @Override
    public ZjAccount getByPhone(String phone) {
        return accountMapper.getByPhone(phone);
    }

    // 更改个人信息
    @Override
    public void updateInfo(AccounLoginInfo info, AccountInfoVo infoVo) {
        ZjAccount account = new ZjAccount();
        account.setId(info.getId());
        account.setName(infoVo.getName());
        account.setNickname(infoVo.getNickname());
        account.setAvatar(infoVo.getAvatar());
        this.updateById(account);

        info.setName(infoVo.getName());
    }

    // 根据账号更新最后登录时间
    @Override
    public void updateLastLoginTime(Long id) {
        ZjAccount account = new ZjAccount();
        account.setId(id);
        account.setLastLoginTime(new Date());
        this.updateById(account);
    }

    // 根据手机号或账号查询
    @Override
    public ZjAccount getByPhoneOrAccount(String phone) {
        return accountMapper.getByPhoneOrAccount(phone);
    }

    // 根据手机号查询
    @Override
    public ZjAccount getPhone(String phone) {
        ZjAccount account = accountMapper.getByPhone(phone);
        return account;
    }

    // 查询或保存账号
    @Override
    public Long getOrSave(Long id, String phone, String password, String name, String nickname) {
        if (null != id && 0 != id) {
            ZjAccount account = new ZjAccount();
            account.setId(id);
            account.setPhone(phone);
            account.setName(name);
            account.setNickname(nickname);
            this.updateById(account);
            return account.getId();
        }
        ZjAccount account = getPhone(phone);
        if (null != account) {
            account.setName(name);
            account.setNickname(nickname);
            this.updateById(account);
            return account.getId();
        }
        account = new ZjAccount();
        account.setId(IdWorker.getId());
        account.setSalt(BCryptUtil.generatorSalt());
        account.setAccount("ZB_" + BCryptUtil.generatorSalt().substring(10, 20));
        account.setPhone(phone);
        account.setPassword(BCryptUtil.encryPassword(SaSecureUtil.md5(StringUtils.isNotBlank(password)? password : phone.substring(5)), account.getSalt()));
        account.setName(name);
        account.setNickname(null == nickname? "" : nickname);
        account.setAvatar("");
        account.setStatus(0);
        this.save(account);
        return account.getId();
    }

    // 根据ID获取部分信息
    @Override
    public List<ZjAccount> getByIds(List<Long> ids) {
        if (null == ids || ids.isEmpty()) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjAccount> wrapper = new QueryWrapper<>();
        wrapper.select("id", "phone", "name", "avatar")
                .in("id", ids);
        return this.list(wrapper);
    }

    // 根据ID获取账号名称
    @Override
    public String getNameById(Long id) {
        ZjAccount account = this.getById(id);
        return null == account? "" : account.getName();
    }

    // 根据账号名称或手机号查询，支持模糊查询
    @Override
    public List<Long> getIdByNameOrPhone(String name, String phone) {
        QueryWrapper<ZjAccount> wrapper = new QueryWrapper<>();
        wrapper.select("id", "phone", "name");
        if (StringUtils.isNotBlank(name)) {
            wrapper.like("name", name);
        }
        if (StringUtils.isNotBlank(phone)) {
            wrapper.like("phone", phone);
        }
        List<ZjAccount> accountList = this.list(wrapper);
        if (CollectionUtils.isEmpty(accountList)) {
            return new ArrayList<>();
        }
        return accountList.stream().map(ZjAccount::getId).toList();
    }

    // 根据账号ID获取姓名
    @Override
    public List<ZjAccount> getNameByIds(List<Long> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjAccount> wrapper = new QueryWrapper<>();
        wrapper.select("id", "name")
                .in("id", ids);
        return this.list(wrapper);
    }

}
